#!/usr/bin/python3.6
# -*- coding: UTF-8 -*-


import os, platform
import time
import argparse
from http.server import HTTPServer, CGIHTTPRequestHandler

workspace_path = os.getcwd()
root_path = workspace_path[0:workspace_path.rfind(os.sep)] + os.sep
root_path = workspace_path + os.sep


def parse_args():
    parser = argparse.ArgumentParser(description='全局修改域名.\n')

    # 黑苹果
    hack = {
        'web_http': 'http://172.22.1.220:9080',
        'res_http': 'https://gitee.com/iTouchTV/hugo_app_distribute/raw/master/a_upload/',
        'ipa_http': 'http://172.22.1.220:9090/files/',
        'hugo_name': '触电新闻'
    }

    # MAC MINI
    mini = {
        'web_http': 'http://172.22.1.114:9080',
        'res_http': 'https://gitee.com/iTouchTV/hugo_app_distribute/raw/master/a_upload/',
        'ipa_http': 'http://172.22.1.114:9090/files/',
        'hugo_name': '触电新闻'
    }

    # Git touchtv
    touchtv = {
        'web_http': 'https://itouchtv.gitee.io/hugo_app_distribute',
        'res_http': 'https://gitee.com/iTouchTV/hugo_app_distribute/raw/master/a_upload/',
        'ipa_http': 'http://172.22.1.220:9090/files/',
        'hugo_name': '触电新闻'
    }

    cos = {
        'web_http': 'https://itouchtv.gitee.io/hugo_app_distribute',
        'res_http': 'https://gitee.com/iTouchTV/hugo_app_distribute/raw/develop/a_upload/',
        'ipa_http': 'https://gz-cos-1252598787.cos.ap-guangzhou.myqcloud.com/ios/ipa_hugo/',
        'hugo_name': '触电新闻'
    }

    src = cos
    tar = hack

    parser.add_argument('-src_name',
                        default=src['hugo_name'],
                        dest='src_name',
                        help='原始blog名称', type=str, required=False)

    parser.add_argument('-src_web_http',
                        default=src['web_http'],
                        dest='src_web_http',
                        help='原始web地址', type=str, required=False)

    parser.add_argument('-src_res_http',
                        default=src['res_http'],
                        dest='src_res_http',
                        help='除IPA外的,原始资源地址,', type=str, required=False)

    parser.add_argument('-src_ipa_http',
                        default=src['ipa_http'],
                        dest='src_ipa_http',
                        help='原始ipa地址', type=str, required=False)

    parser.add_argument('-tar_name',
                        default=src['hugo_name'],
                        dest='tar_name',
                        help='目标blog名称', type=str, required=False)

    parser.add_argument('-tar_web_http',
                        default=tar['web_http'],
                        dest='tar_web_http',
                        help='目标web地址', type=str, required=False)

    parser.add_argument('-tar_res_http',
                        default=tar['res_http'],
                        dest='tar_res_http',
                        help='除IPA外的,目标资源地址,', type=str, required=False)

    parser.add_argument('-tar_ipa_http',
                        default=tar['ipa_http'],
                        dest='tar_ipa_http',
                        help='目标ipa地址', type=str, required=False)

    args = parser.parse_args()
    return args


class Ipa(object):
    def __init__(self):
        pass

    # 递归文件
    def list_files(self, path, list_name):  # 传入存储的list
        for file in os.listdir(path):
            file_path = os.path.join(path, file)
            if os.path.isdir(file_path):
                self.list_files(file_path, list_name)
            else:
                list_name.append(file_path)

    # 读取文件
    def load_file(self, path):

        f = open(path, 'r')
        sourceInLine = f.read()
        f.close()
        return sourceInLine

        f = open(path, 'r')
        sourceInLine = f.read()
        lines = []
        for line in sourceInLine:
            lines.append(line)
        f.close()
        return lines
        # dataset=[]
        # for line in sourceInLine:
        #     temp1=line.strip('\n')
        #     temp2=temp1.split('\t')
        #     dataset.append(temp2)
        # return dataset

    # 保存文件
    def save_file(self, file_path, lines):
        if (os.path.exists(file_path)):
            os.remove(file_path)
        file = open(file_path, 'w')
        file.write(lines)
        file.close()
        return
        if (os.path.exists(file_path)):
            os.remove(file_path)
        file = open(file_path, 'w')
        file.writelines(lines)
        file.close()


def run():
    startTime = time.time()

    print('run in %s system' % (platform.system()))

    app_args = parse_args()
    src_web_http = app_args.src_web_http
    src_res_http = app_args.src_res_http
    src_ipa_http = app_args.src_ipa_http
    src_name = app_args.src_name
    tar_web_http = app_args.tar_web_http
    tar_res_http = app_args.tar_res_http
    tar_ipa_http = app_args.tar_ipa_http
    tar_name = app_args.tar_name
    list_file = []
    tool = Ipa()
    tool.list_files(root_path, list_file)
    for path in list_file:
        if path.find('venv') < 0 \
                and path.find('.DS_Store') < 0 \
                and path.find('change_config.py') < 0 \
                and path.find('conten') < 0 \
                and path.find('a_upload') < 0 \
                and path.find('public') < 0:
            try:
                lines = tool.load_file(path)
                lines = lines.replace(src_res_http, tar_res_http) \
                    .replace(src_ipa_http, tar_ipa_http) \
                    .replace(src_web_http, tar_web_http) \
                    .replace(src_name, tar_name)
                tool.save_file(path, lines)
            except:
                pass

    endTime = time.time()
    print("总耗时: %0.2f" % (endTime - startTime))


if __name__ == '__main__':
    run()
